package org.wu.framework.lazy.orm.core.persistence.reverse.lazy.vue;

import lombok.extern.slf4j.Slf4j;
import org.wu.framework.core.CamelAndUnderLineConverter;
import org.wu.framework.core.NormalUsedString;
import org.wu.framework.lazy.orm.core.config.ReverseEngineering;
import org.wu.framework.lazy.orm.core.persistence.conf.LazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.persistence.conf.field.DefaultLazyTableFieldEndpoint;
import org.wu.framework.lazy.orm.core.persistence.format.FormatFactory;
import org.wu.framework.lazy.orm.core.persistence.reverse.ReverseClassLazyTableEndpoint;
import org.wu.framework.translation.data.JavaKeyword;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 用于生成api
 */
@Slf4j
public class DefaultVueApi extends DefaultAbstractVue {
    protected DefaultVueApi(ReverseClassLazyTableEndpoint reverseClassLazyTableEndpoint, ReverseEngineering reverseEngineering) {
        super(reverseClassLazyTableEndpoint, reverseEngineering);
    }

    /**
     * description:  添加 class
     *
     * @author 吴佳伟
     * @date: 13.2.23 17:13
     */
    @Override
    public void initClassNamePart() {

        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        String jsName = tableEndpoint.getClassName();
        setFileName(String.format("%sApi", jsName));
        super.initClassNamePart();
    }

    /**
     * 创建Vue 文件内容
     *
     * @return Vue文件内容
     */
    @Override
    protected String doCreateVueContextCode() {
        // 读取文件模版
        String fileName = "vue/api.js";
        String format = getFormatContent(fileName);
        log.info("api.js:{}", format);
        // 设置参数
        Map<String, Object> arguments = new HashMap<>();

        // 获取字段
        List<LazyTableFieldEndpoint> outLazyTableFieldEndpointList = this.getReverseClassLazyTableEndpoint().getOutLazyTableFieldEndpoints();

        List<DefaultLazyTableFieldEndpoint> defaultLazyTableFieldEndpointList = outLazyTableFieldEndpointList.stream().map(lazyTableFieldEndpoint -> {

            DefaultLazyTableFieldEndpoint defaultLazyTableFieldEndpoint = new DefaultLazyTableFieldEndpoint();
            defaultLazyTableFieldEndpoint.setName(lazyTableFieldEndpoint.getName());
            defaultLazyTableFieldEndpoint.setComment(lazyTableFieldEndpoint.getComment());
            defaultLazyTableFieldEndpoint.setDefaultValue(lazyTableFieldEndpoint.getDefaultValue());
            return defaultLazyTableFieldEndpoint;
        }).toList();

        String module_comment = this.getReverseClassLazyTableEndpoint().getComment();
        String className = this.getReverseClassLazyTableEndpoint().getClassName();
        String class2LowName = this.getReverseClassLazyTableEndpoint().getClass2LowName();

        arguments.put("columnList", defaultLazyTableFieldEndpointList);

        arguments.put("module_comment", module_comment);
        arguments.put("module", class2LowName);
        arguments.put("Module", className);


        ReverseClassLazyTableEndpoint tableEndpoint = this.getReverseClassLazyTableEndpoint();
        String domain = tableEndpoint.getClassName();
        List<String> domainNames = CamelAndUnderLineConverter.humpToArray(domain);
        String path = domainNames.stream().map(domainName -> {
            if (Arrays.stream(JavaKeyword.values()).anyMatch(javaKeyword -> javaKeyword.name().toLowerCase().equals(domainName))) {
                return domainName + NormalUsedString.UNDERSCORE;
            }
            return domainName;
        }).collect(Collectors.joining(NormalUsedString.SLASH));
        addClassAnnotationPart(String.format("@Tag(name = \"%s提供者\")", this
                .getReverseClassLazyTableEndpoint().getComment()));


        arguments.put("api_prefix", String.format("%s/%s", this.getReverseEngineering().getApiUrlPrefix(), path));
        // 格式化
        String formatted = FormatFactory.format(format, arguments);
        return formatted;

    }


    /**
     * 获取 文件module type
     *
     * @return
     */
    @Override
    protected String getModuleType() {
        return "view";
    }

    /**
     * 获取文件后缀
     *
     * @return 文件后缀
     */
    @Override
    protected String getFileSuffix() {
        return ".js";
    }
}
